package net.jarang.android.support.http;

import java.util.Map;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

abstract public class HttpInvocator {
	protected int connectionTimeout;
	protected int soTimeout;
	protected String paramEncoding;
	protected String responseEncoding;
	
	
	public HttpInvocator(int connectionTimeout, int soTimeout, String paramEncoding) {
		this.connectionTimeout = connectionTimeout;
		this.soTimeout = soTimeout;
		this.paramEncoding = paramEncoding;
	}
	
	abstract public HttpResult get(String url, Map<String, Object> params, Map<String, String> headers);
	abstract public HttpResult post(String url, Map<String, Object> params, Map<String, String> headers);
	
	protected HttpClient getHttpClient() {
		HttpClient client = (HttpClient) HttpClientTemplate.getClient(connectionTimeout, soTimeout);
		return client;
	}
	
	protected HttpClient getHttpClient(String host, int port) {
		HttpClient client = HttpClientTemplate.getClient(host, port, connectionTimeout, soTimeout);
		return client;
	}	
	
	protected GetMethod getMethod(String uri, Map<String, Object> params) {
		return getMethod(uri, params, null);
	}
	
	protected GetMethod getMethod(String uri, Map<String, Object> params, Map<String, String> headers) {
		GetMethod method = HttpMethodTemplate.getMethod(uri, params, headers, paramEncoding);
		return method;
	}
	
	protected PostMethod postMethod(String uri, Map<String, Object> params) {
		PostMethod method = HttpMethodTemplate.postMethod(uri, params, paramEncoding);
		return method;
	}
	
	protected PostMethod postMethod(String uri, Map<String, Object> params, Map<String, String> headers) {
		PostMethod method = HttpMethodTemplate.postMethod(uri, params, headers, paramEncoding);
		return method;
	}
	
	protected PostMethod postMethodForMultipart(String uri, Map<String, Object> params, Map<String, String> headers) {
		PostMethod method = HttpMethodTemplate.postMethodForMultipart(uri, params, paramEncoding);
		return method;
	}
	
	protected String getResponseEncoding(HttpMethodBase method) {
		String returnValue = method.getResponseCharSet();

		if (returnValue == null) {
			returnValue = responseEncoding;
		}
		return returnValue;
	}	
	
	protected void responseStatus(int statusCode) {
		if ((statusCode / 100) != 2) {
			throw new HttpCallException("Unexcepted result! status code is " + statusCode);
		}
	}
	
	protected void releaseConnection(HttpMethod method) {
		if (method != null) {
			method.releaseConnection();
		}		
	}
	
	protected void checkCookie(HttpClient client, int cookieLength) {
		Cookie[] logonCookies = client.getState().getCookies();
		
		if (logonCookies.length < cookieLength) {
			throw new IllegalAccessError("login error" + " cookies do not create");
		}
	}
	
	public String getParamEncoding() {
		return paramEncoding;
	}

	public void setParamEncoding(String paramEncoding) {
		this.paramEncoding = paramEncoding;
	}

	public int getSoTimeout() {
		return soTimeout;
	}

	public void setSoTimeout(int soTimeout) {
		this.soTimeout = soTimeout;
	}

	public int getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(int connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

	public String getResponseEncoding() {
		return responseEncoding;
	}

	public void setResponseEncoding(String resultEncoding) {
		this.responseEncoding = resultEncoding;
	}	
}
